package com.laoyao.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.Test;

public class DateUtil {
	/**
	 * 
	 * 输入自定义时间格式，返回当前日期，列如输入"yyyy-MM-dd HH:mm:ss"
	 * @param pattern 分割符号
	 * @return
	 */
	
	public static String getDate(String pattern){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(new Date()); 
	}
	/**
	 * 默认返回格式yyyy-MM-dd
	 * @return
	 */
	public static String getDate(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date()); 
	}
	/**
	 * 获取当前时间格式为 yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getTime(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date()); 
	}
	/**
	 * 函数名够直接了，自己体会
	 * @return
	 */
	public static String getyyyyMMddHHmmssSSS(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return sdf.format(new Date()); 
	}
	/**
	 * 如果加天天数就输入正整数，反之输入负数
	 * @param num 增加日期的天数
	 * @return 返回增加后的日期 例：2018-08-13 14:14:17
	 */
	
	public static  String addDay(int num){
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		c.add(c.DATE, num);
		return sdf.format(c.getTime());
	}
	/**
	 * 
	 * @param date 当前日期
	 * @param num 增加天数
	 * @return
	 */
	public static  String addDay(String date,int num){
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			c.setTime(sdf.parse(date));
			c.add(c.DATE, num);
			return sdf.format(c.getTime());
		} catch (ParseException e) {
	        
			e.printStackTrace();
			return null;
		}
	}
	public static  String addDay(String date,int num,String patten){
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(patten);
		try {
			c.setTime(sdf.parse(date));
			c.add(c.DATE, num);
			return sdf.format(c.getTime());
		} catch (ParseException e) {
	        
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * @param num 如果加天数就输入正整数，反之输入负数
	 * @return 返回修改后的日期不带时间 例：2018-08-13
	 */
	public static String addyyyyMMdd(int num){
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		c.add(c.DATE, num);
		return sdf.format(c.getTime());
	}
	
	/**
	 * 
	 * @param date 当前日期
	 * @param num 增加天数
	 * @return
	 */
	public static String addyyyyMMdd(String date,int num){
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			c.setTime(sdf.parse(date));
			c.add(c.DATE, num);
			return sdf.format(c.getTime());
		} catch (ParseException e) {
	        
			e.printStackTrace();
			return null;
		}
		
	}
	/**
	 * 获取当前年月
	 * @return
	 */
	public static String getNowYearMonth(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		return sdf.format(new Date()); 
	}
	
	/**
	 * 比较两个时间大小，arg1>arg2返回1
	 * arg1<arg2返回-1
	 * arg1==agr2返回0
	 * 出错返回2
	 * @param arg1
	 * @param arg2
	 * @return
	 */
	public static int dateCmp(String arg1,String arg2){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long time1=0;
		long time2=0;
		try {
			 time1 = sdf.parse(arg1).getTime();
			 time2 = sdf.parse(arg2).getTime();		
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 2;
		}
		
		if(time1==time2)
			return 0;
		if(time1>time2)
			return 1;

		return -1;
		
	}
	/**
	 * 判断当前时间是否超过某天的某一时间
	 * @param time
	 * @return
	 */
	public  static boolean overTodayTime(String date,long seconds){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		long time = 0;
		
		try {
			time = sdf.parse(date).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		time=time+seconds*1000;
		
		return new Date().getTime()>=time;
	}
	/**
	 * 获取给定日期的周几
	 * @param date 要转换的日期，格式yyyy-MM-dd
	 * @return
	 */
	public static Integer getDay(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		try {
			return sdf.parse(date).getDay();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}
	
	/**
	 * 获取当前时间距离yyyy-MM-dd多少个7天
	 * @param date
	 * @return
	 */
	public static int getWeekNum(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Long weekNum;
		 try {
			weekNum=(new Date().getTime()-sdf.parse(date).getTime())/(7*24*3600*1000);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
		
		return weekNum.intValue();
	}
	
	/**
	 * yyyy-MM-dd从yyyy-MM-dd相差几个7天
	 * @param date
	 * @return
	 */
	public static int getWeekNum(String date1,String date2){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Long weekNum;
		 try {
			weekNum=(sdf.parse(date1).getTime()-sdf.parse(date2).getTime())/(7*24*3600*1000);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
		
		return weekNum.intValue();
	}
	/**
	 * 以秒为单位增加当前天数时间
	 */
	public static String addTime(int second){
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		c.add(c.SECOND, second);
		return sdf.format(c.getTime());
	}
	
	
	@Test
	public void test(){
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		 try {
			System.out.println((new Date().getTime()-sdf.parse("2018-12-24").getTime())/(7*24*3600*1000));
			System.out.println(sdf.parse("2018-12-24"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	

}
